package cn.itsource.pet.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import cn.itsource.basic.domain.AjaxResult;
import cn.itsource.basic.domain.BaseQuery;
import cn.itsource.basic.util.GetUserInfoUtil;
import cn.itsource.basic.util.PageList;

import cn.itsource.basic.util.PhoneUtil;
import cn.itsource.basic.util.baidu.DistanceUtil;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.domain.User;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.org.mapper.UserMapper;
import cn.itsource.pet.domain.*;
import cn.itsource.pet.mapper.OrderPetAcquisitionMapper;
import cn.itsource.pet.mapper.PetTypeMapper;
import cn.itsource.pet.mapper.SearchMasterMessageMapper;
import cn.itsource.pet.mapper.SearchMasterMsgMapper;
import cn.itsource.pet.service.SearchMasterMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class SearchMasterMessageServiceImpl implements SearchMasterMessageService {
    @Autowired
    SearchMasterMessageMapper searchMasterMessageMapper;
    @Autowired
    PetTypeMapper petTypeMapper;
    @Autowired
    SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    ShopMapper shopMapper;
    @Autowired
    OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    EmployeeMapper employeeMapper;

    @Override
    public void insert(SearchMasterMessage searchMasterMessage) {
        searchMasterMessageMapper.insertSelective(searchMasterMessage);
    }

    @Override
    public void update(SearchMasterMessage searchMasterMessage) {

    }

    @Override
    public PageList<SearchMasterMessage> findPage(BaseQuery baseQuery, String key) {
        return null;
    }

    @Override
    public void delete(Long id) {

    }

    @Override
    public void deleteBatch(List<Long> ids) {

    }

    @Override
    public List<SearchMasterMessage> findAll() {
        return null;
    }

    @Override
    public SearchMasterMessage findOne(Long id) {
        return null;
    }

    @Override
    public PageList<SearchMasterMessage> pagelist(SearchMasterMessageQuery query,Long shopId) {
        PageList<SearchMasterMessage> tPageList = new PageList<>();
        Shop shop = shopMapper.selectByPrimaryKey(shopId);
        List<SearchMasterMessage> list = searchMasterMessageMapper.pagelist(query, shopId);
        if (CollectionUtil.isEmpty(list)) return tPageList;
        //java8新特性的过滤器
        List<SearchMasterMessage> filterCollect = list.stream().
                //这里是先做过滤 获取到属于当前半径内的 所有寻主单号
                        filter(searchMasterMsg -> DistanceUtil.isInCircle(shop.getAddress(), searchMasterMsg.getAddress(), 5000)).
                        collect(Collectors.toList());
        //分页过滤以后的数据
        //如果过滤后为空
        if (CollectionUtil.isEmpty(filterCollect)) return tPageList;
        //这是做设置分页的起始位置
        List<SearchMasterMessage> PageCollect = filterCollect.stream().
                skip((query.getPage() - 1) * query.getPageSize()).
                //这里是设置每次查询多少条
                        limit(query.getPageSize()).
                        collect(Collectors.toList());
        tPageList.setTotal((long) filterCollect.size());
        tPageList.setList(PageCollect);
        return tPageList;
    }

    @Override
    public AjaxResult shenghe(SearchMasterMessage msg) {

        return AjaxResult.me();
    }

    @Override
    public AjaxResult order(SearchMasterMessage message) {
        //第一步修改状态把shopid存进去
        Long shopId = GetUserInfoUtil.getShopId();
        message.setShopId(shopId);
        searchMasterMessageMapper.updateByPrimaryKeySelective(message);
        //给用户发送接单短信
        //通过用户id和员工id查到对应信息发送消息
        User user = userMapper.selectByPrimaryKey(message.getUserId());
        Employee employee = employeeMapper.selectByPrimaryKey(message.getEmplId());
        PhoneUtil.email2(user.getPhone(), user.getUsername(), employee.getUsername());
        //第二步将数据存入派单表
        SearchMasterMessage searchMasterMessage = searchMasterMessageMapper.selectByPrimaryKey(message.getId());
        OrderPetAcquisition orderPetAcquisition = BeanUtil.copyProperties(searchMasterMessage, OrderPetAcquisition.class);
        orderPetAcquisition.setDigest(message.getTitle());
        orderPetAcquisition.init();
        orderPetAcquisition.setEmpId(message.getEmplId());
        orderPetAcquisitionMapper.insertSelective(orderPetAcquisition);
        return AjaxResult.me();
    }
}
